package org.jground.rx.ui.zk;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RXClass;
import org.jground.rx.RXPage;
import org.jground.rx.RXPanel;
import org.jground.rx.RXSystem;
import org.jground.rx.ui.IUIAttribute;
import org.jground.rx.ui.pref.FocusData;
import org.jground.util.CollectionUtils;
import org.jground.util.L;
import org.jground.zk.zulX.PanelX;
import org.jground.zk.zulX.TabboxX;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Label;

public class UIViewPanel {

    private static Log logger = LogFactory.getLog(UIViewPanel.class);

    protected UIFrame uiFrame;
    protected RXClass moClass;
    protected IRXObject moDataObject;
    protected List<IUIAttribute<Component, UIFrame>> uiAttributes = new ArrayList<IUIAttribute<Component, UIFrame>>();
    protected RXPanel rxPanel;
    protected List<UIPage> uiPages = new ArrayList<UIPage>();

    protected Component ui;

    public UIViewPanel(UIFrame uiFrame, String moid) {
        this.uiFrame = uiFrame;
        this.moClass = RXSystem.getInstance().getRxService().getRXClass(moid);
        init();
    }

    protected void init() {
        L.debug(logger, "init of UIViewPanel", this.moClass.getMoid());
        rxPanel = moClass.getViewRXPanel();
        createPages();
    }

    protected void createPages() {
        if (rxPanel == null) {
            logger.warn("no RXPanel object found in class: " + moClass);
            ui = new Label("no panel found");
        } else {
            List<RXPage> rxPages = rxPanel.getRXPages();

            //
            // no tabs
            //
            if (rxPages.size() == 1) {
                String title = rxPages.get(0).getLabel();
                PanelX panel = new PanelX();
                panel.setTitle(title);
                ui = panel;
                panel.setStyle("border:red dotted 0px");
                UIPage uiPage = new UIPage(uiFrame, rxPages.get(0), uiAttributes);
                uiPages.add(uiPage);
                // panel.add(uiPage.getView());
                ui = uiPage.getView();
            }
            //
            // tabs
            //
            else {
                TabboxX tabbox = new TabboxX();
                tabbox.setStyle("border:red dotted 0px");
                ui = tabbox;
                for (RXPage moPage : rxPages) {
                    UIPage uiPage = new UIPage(uiFrame, moPage, uiAttributes);
                    uiPages.add(uiPage);
                    if (!uiPage.isEmpty()) {
                        tabbox.addTab(uiFrame, moPage.getLabel(), moPage.getDescription(), uiPage.getView());
                        L.debug(logger, moPage.getLabel(), "added");
                    }
                }
            }
        }
    }

    public void setData(IRXObject moDataObject) {
        this.moDataObject = moDataObject;
        for (IUIAttribute<Component, UIFrame> uiAttribute : uiAttributes) {
            uiAttribute.clearData();
            uiAttribute.setData(moDataObject);
        }
    }

    public void clearData() {
        for (IUIAttribute<Component, UIFrame> uiAttribute : uiAttributes) {
            uiAttribute.clearData();
        }
        moDataObject = null;
    }

    public Component getView() {
        return ui;
    }

    public FocusData getFocusData() {
        int index = 0;
        if (ui instanceof TabboxX) {
            index = ((TabboxX) ui).getSelectedIndex();
        }
        index = index < 0 ? 0 : index;
        UIPage uiPage = uiPages.get(index);
        FocusData fd = uiPage.getFocusData();
        fd.push(index);
        return fd;
    }

    public void setFocusData(FocusData fd) {
        if (CollectionUtils.isNotEmpty(fd)) {
            Object o = fd.pop();
            if (o instanceof Integer && ui instanceof TabboxX) {
                int index = (Integer) o;
                TabboxX tabbox = (TabboxX) ui;
                tabbox.setSelectedIndex(index);
                uiPages.get(index).setFocusData(fd);
            }
        }
    }
}